<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>
        人员添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/css/main.css}" media="all">
    <script type="text/javascript" th:src="@{/lib/loading/okLoading.js}"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" th:href="@{/css/index.css}">
    <style>
        .imgs {
            display: none;
        }

        .picture {
            display: none;
        }
    </style>

</head>
<body>
<div id="app">
    <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>业主信息管理</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <el-form :model="owner" :rules="rules" ref="buildingFormRef" label-width="120px" class="demo-ruleForm">

            <el-form-item label="所属小区" prop="communityName">
                <el-select v-model="owner.communityName" filterable placeholder="--请选择所属小区--">
                    <el-option
                            v-for="item in communityList"
                            :key="item.index"
                            :label="item.name"
                            :value="item.name" @click.native="houseFindAllByName(owner.communityName)">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="成员名称" prop="name">
                <el-input v-model="owner.name"></el-input>
            </el-form-item>

            <el-form-item label="身份证号" prop="idcard">
                <el-input v-model="owner.idcard"></el-input>
            </el-form-item>

            <el-form-item label="出生日期" prop="birthday">
                <el-date-picker
                        v-model="owner.birthday"
                        type="date"
                        placeholder="选择日期">
                </el-date-picker>
            </el-form-item>

            <el-form-item label="联系方式" prop="telephone">
                <el-input v-model="owner.telephone"></el-input>
            </el-form-item>

            <el-form-item label="职业" prop="profession">
                <el-input v-model="owner.profession"></el-input>
            </el-form-item>

            <el-form-item label="性别" prop="sex">
                <el-radio v-model="owner.sex" label="0" border>男</el-radio>
                <el-radio v-model="owner.sex" label="1" border>女</el-radio>
            </el-form-item>

            <el-form-item label="成员类型" prop="type">
                <el-radio-group v-model="owner.type">
                    <el-radio label="0" border>业主</el-radio>
                    <el-radio label="1" border>家庭成员</el-radio>
                    <el-radio label="2" border>租户</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="成员照片" prop="thumbnail">
                <el-upload
                        class="upload-demo"
                        action="/estate/fileupload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-success="handleSuccess"
                        :file-list="fileList"
                        list-type="picture">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10M</div>
                </el-upload>
            </el-form-item>

            <el-form-item label="描述" prop="remark">
                <el-input type="textarea" v-model="owner.remark"></el-input>
            </el-form-item>

            <el-form-item>
                <el-input v-model="owner.communityId"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="save()" style="text-align: center">保存</el-button>
                <el-button @click="clearAll()" style="text-align: center">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/x-layui.js}" charset="utf-8"></script>
<script th:src="@{/js/jquery.min.js}" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script th:src="@{/js/vue.js}" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script th:src="@{/js/index.js}"></script>
<!--导入Axios依赖包-->
<script th:src="@{/js/axios.js}" charset="utf-8"></script>

<script type="text/javascript">
    var app = new Vue({
        el: "#app",
        data: {
            owner: {type: '', sex: ''},
            title: "添加人员管理",
            owner: {picture: "", id: "",communityId:""},
            communityList: [],
            fileList: [],
            rules: {
                communityName: [
                    {required: true, message: '请选择所属小区名称', trigger: 'blur'}
                ],
                thumbnail:[
                    { required: true, message: '请上传人员缩略图', trigger: 'change' }
                ],
                name: [
                    {required: true, message: '成员名称是必填项，岂能为空？', trigger: 'blur'}
                ],
                idcard: [
                    {required: true, message: '请输入身份证号', trigger: 'blur'}
                ],
                telephone: [
                    {required: true, message: '请输入联系方式', trigger: 'blur'}
                ],
                birthday: [
                    {required: true, message: '请选择出生日期', trigger: 'blur'}
                ],
                profession: [
                    {required: true, message: '请输职业', trigger: 'blur'}
                ],
                remark: [
                    {required: true, message: '请输入描述', trigger: 'blur'}
                ],
                sex: [
                    {required: true, message: '请选择性别', trigger: 'blur'}
                ],
                type: [
                    {required: true, message: '请选择成员类型', trigger: 'blur'}
                ],
                thumbnail: [
                    {required: true, message: '请上传人员缩略图', trigger: 'change'}
                ]
            }
        },
        methods: {
            houseFindAllByCommunityName() {
                axios.get("/community/find").then((res) => {
                    this.communityList = res.data.data;
                });
            },
            houseFindAllByName(communityName){
                axios.get("/home/addById?communityName=" + communityName).then((res) => {
                    this.owner.communityId = res.data.data+"";
                });
            },
            findById() {
                axios.get("/owner/findById?id=" + this.owner.id).then((res) => {
                    this.owner = res.data.data;
                    this.fileList.push({url: "http://localhost/estate" + this.owner.picture});
                });
            },
            handleSuccess(response, file, fileList) {
                this.owner.picture = response.data;
            },
            handleRemove() {
                axios.get("/estate/delfile?fileName=" + this.owner.picture).then((res) => {
                    app.owner.picture = "";
                    if (res.data.flag) {
                        app.fileList = [];
                        this.$message({
                            message: '图片删除成功',
                            type: 'success'
                        });
                    }
                });
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            //表达重置
            clearAll() {
                this.$refs.buildingFormRef.resetFields();
                if (this.fileList.length > 0) {
                    this.handleRemove();
                }
            },
            save() {
                var method = "";
                if (this.owner.id != null) {
                    method = "update"
                } else {
                    method = "add"
                }
                axios.post("/owner/" + method, this.owner).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                });
            },
            getQueryString(name) {
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
        },
        created: function () {
            layui.use(['element', 'layer', 'form'], function () {
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数，小区ID
            this.owner.id = this.getQueryString("id");
            //根据小区ID获取小区对象，进行数据回显
            if (this.owner.id != null && this.owner.id != '') {
                this.title = "修改人员管理";
                this.findById();
            }
            this.houseFindAllByCommunityName();
        }
    });
</script>
</body>
</html>